//////////////////////////////////////////////////////////////////////////

#ifndef _IMAGES_H_
#include "images.h"
#endif

//////////////////////////////////////////////////////////////////////////

#include <SDL\SDL.h>
#include <SDL\SDL_image.h>
#include "SDL_rotozoom.h"

//////////////////////////////////////////////////////////////////////////


GUI_Images::GUI_Images(const std::string& _resourcesFolder, float _resizeFactor):
    m_GUIResolution(960),
    m_MapResolution(1280),
    m_ImagesSubfolder("\\Images\\"),
    m_MapsSubfolder("\\Maps\\")
{
    m_ResourcesFolder = _resourcesFolder;

    memset(m_Images, 0, sizeof(m_Images));
    Resize(_resizeFactor);

    m_Images[Empty] = 0;
}


//////////////////////////////////////////////////////////////////////////


GUI_Images::~GUI_Images()
{
    //delete [] m_ImagesFolder;
    for (int i = 0; i < ImageCount; ++i)
    {
        if (m_Images[i]) 
            SDL_FreeSurface(m_Images[i]);
    }
}


//////////////////////////////////////////////////////////////////////////


void GUI_Images::Resize(float _resizeFactor)
{
    m_ResizeFactor = _resizeFactor;

    InitImages();

    ReloadMapImage(GUI_Images::PlayerHighlight, "highlight.png");
    ReloadMapImage(GUI_Images::GameBullet, "bullet.png");
    ReloadMapImage(GUI_Images::GameWall, "wall.png");
    ReloadMapImage(GUI_Images::MapBackground, "background.jpg");
    ReloadMapImage(GUI_Images::MapPreview, "preview.png");
}


//////////////////////////////////////////////////////////////////////////


void GUI_Images::ReloadMapImages(const std::string& _mapFolder)
{
    m_CurrentMap = _mapFolder;
    m_CurrentMap += "\\";
    ReloadMapImage(GUI_Images::PlayerHighlight, "highlight.png");
    ReloadMapImage(GUI_Images::GameBullet, "bullet.png");
    ReloadMapImage(GUI_Images::GameWall, "wall.png");
    ReloadMapImage(GUI_Images::MapBackground, "background.jpg");
    ReloadMapImage(GUI_Images::MapPreview, "preview.png");
}


//////////////////////////////////////////////////////////////////////////


void GUI_Images::InitImages()
{
    ReloadImage(GUI_Images::ButtonGreen, "green.png");
    ReloadImage(GUI_Images::ButtonGreenHover, "green_hover.png");
    ReloadImage(GUI_Images::ButtonRed, "red.png");
    ReloadImage(GUI_Images::ButtonRedHover, "red_hover.png");
    ReloadImage(GUI_Images::Edit, "edit.png");
    ReloadImage(GUI_Images::EditHover, "edit_hover.png");
    ReloadImage(GUI_Images::ButtonLeft, "left.png");
    ReloadImage(GUI_Images::ButtonLeftHover, "left_hover.png");
    ReloadImage(GUI_Images::ButtonRight, "right.png");
    ReloadImage(GUI_Images::ButtonRightHover, "right_hover.png");
    ReloadImage(GUI_Images::Checked, "checked.png");
    ReloadImage(GUI_Images::Unchecked, "unchecked.png");
    ReloadImage(GUI_Images::Background, "background.jpg");

    ReloadImage(GUI_Images::Player1, "Players\\1.png");
    ReloadImage(GUI_Images::Player2, "Players\\2.png");
    ReloadImage(GUI_Images::Player3, "Players\\3.png");
    ReloadImage(GUI_Images::Player4, "Players\\4.png");
    ReloadImage(GUI_Images::Player5, "Players\\5.png");
    ReloadImage(GUI_Images::Player6, "Players\\6.png");

    ReloadImage(GUI_Images::HUDHeart, "heart.png");
    ReloadImage(GUI_Images::MenuBack, "menu_back.png");
    ReloadImage(GUI_Images::HUDBack, "hud_back.png");
    
}


//////////////////////////////////////////////////////////////////////////


void GUI_Images::ReloadMapImage(GUI_Images::Image _image, const std::string& _fileName)
{
    ReloadImage(_image, m_ResourcesFolder + m_MapsSubfolder + m_CurrentMap + _fileName, m_ResizeFactor / m_MapResolution,  m_ResizeFactor / m_MapResolution);
}


//////////////////////////////////////////////////////////////////////////


void GUI_Images::ReloadImage(GUI_Images::Image _image, const std::string& _fileName)
{
    ReloadImage(_image, m_ResourcesFolder + m_ImagesSubfolder + _fileName, m_ResizeFactor / m_GUIResolution,  m_ResizeFactor / m_GUIResolution);
}


//////////////////////////////////////////////////////////////////////////


void GUI_Images::ReloadImage(GUI_Images::Image _image, const std::string& _fileName, float _width, float _height)
{
    if (m_Images[_image]) 
        SDL_FreeSurface(m_Images[_image]);

   //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized surface that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = IMG_Load(_fileName.c_str());

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized surface
        optimizedImage = SDL_DisplayFormatAlpha( loadedImage );

        //Free the old surface
        SDL_FreeSurface( loadedImage );
    }

    m_Images[_image] = zoomSurface(optimizedImage, _width, _height, 1);

    SDL_FreeSurface(optimizedImage);
}


//////////////////////////////////////////////////////////////////////////


SDL_Surface *GUI_Images::Get(GUI_Images::Image _imageName)
{
    return m_Images[_imageName];
}


//////////////////////////////////////////////////////////////////////////
